#include "planet.h"

Planet::Planet(int x, int y, const char* textureName)
{
	position = Vector2(x, y);
	this->textureName = textureName;

	SetupImages();

	boundingBox = BoundingBox(position, width, height);

	//Setup landing position of ufo based on planet position
	int displayWidth = Iw2DGetSurfaceWidth();
	int displayHeight = Iw2DGetSurfaceHeight();
	if(position.x > displayWidth / 2)
	{
		landPosition.x = position.x - widthHalf/2;
		landPosition.y = position.y;
	}
	else if(position.x < displayWidth / 2)
	{
		landPosition.x = position.x + widthHalf/2;
		landPosition.y = position.y;
	}
	else if(position.y < displayHeight / 2)
	{
		landPosition.x = position.x;
		landPosition.y = position.y + heightHalf/2;
	}
	else if(position.y > displayHeight / 2)
	{
		landPosition.x = position.x;
		landPosition.y = position.y - heightHalf/2;
	}
	else
	{
		landPosition.x = position.x;
		landPosition.y = position.y;
	}
}

Planet::~Planet()
{
	CleanupImages();
}

void Planet::SetupImages()
{
	planetSprite = Iw2DCreateImageResource(textureName);
	width  = planetSprite->GetWidth();
    height = planetSprite->GetHeight();
	widthHalf = width / 2;
	heightHalf = height / 2;
}

void Planet::CleanupImages()
{
	if(planetSprite != NULL)
	{
		delete planetSprite;
		planetSprite = NULL;
	}
}

void Planet::Render()
{
	Iw2DDrawImage(planetSprite, CIwSVec2((int16)position.x - widthHalf, (int16)position.y - heightHalf));
}

void Planet::Update(int deltaTimeMs)
{
}